package iotRemote

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commMqtt"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Uuid"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/periodTask"
	"gitee.com/fierce_wolf/go-fox-edge-iot-common/iotAPI"
)

type remoteMqttService struct {
	GateWayMode    bool
	defaultOptions map[string]interface{}
	ClientId       string
	Client         *commMqtt.MqttClient
}

func (e *remoteMqttService) newClient(mqttParam map[string]interface{}) {
	clientId := Map.GetString(mqttParam, "clientId", "")
	if clientId == "" {
		clientId = Uuid.RandomUUID()
	}
	Mqtt.ClientId = clientId
	Mqtt.Client = commMqtt.NewClient(Mqtt.ClientId)
}

func (e *remoteMqttService) setPublicConfig(mqttParam map[string]interface{}) {
	e.defaultOptions = mqttParam
}

func (e *remoteMqttService) setMqttConfig(mqttParam map[string]interface{}) {
	e.Client.SetOption(mqttParam)

	// 向MQTT BROKER注册订阅topic
	e.Client.Subscribe(iotAPI.MqttHandler.GetTopicFunc(), iotAPI.MqttHandler.MqttHandlerFunc())

	// 添加任务
	periodTask.InsertTask(commMqtt.AutoTask)
}

func (e *remoteMqttService) Publish(topic string, payload interface{}, timeout int64) error {
	return e.Client.Publish(topic, payload, timeout)
}
